package com.yanqu.road.server.cmd.activity.tonbingchannel;


import com.yanqu.road.entity.activity.tonbingchannel.config.ChannelTonBingConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ChannelTonBingProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;

import com.yanqu.road.server.manger.activity.tonbingchannel.ChannelTonBingMgr;
import com.yanqu.road.server.protocol.*;

@Cmd(code = GameProtocol.S_CTON_BING_GET_RANK_REWARD, desc = "")
public class TonBingGetRankRewardCmd extends AbstractCommand {

    private ChannelTonBingProto.CTonBingGetRankRewardReqMsg reqMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = ChannelTonBingProto.CTonBingGetRankRewardReqMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ChannelTonBingConfig config = ChannelTonBingMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        if (!ActivityMgr.activityInCanRewardPeriod(config.getActivityInfo())) {
            ret = GameErrorCode.E_TON_BING_NO_OPERATE_TIME;
            return;
        }

        if (!ChannelTonBingMgr.isAble(player.getUserId())) {
            ret = GameErrorCode.E_TON_BING_NO_ABLE;
            return;
        }

        ChannelTonBingProto.ChannelTonBingGetRankRewardReqMsg.Builder builder = ChannelTonBingProto.ChannelTonBingGetRankRewardReqMsg.newBuilder();
        builder.setActivityId(config.getActivityInfo().getActivityId());
        builder.setReq(reqMsg.toBuilder());
        player.sendChannelPlayerPacket(ChannelProtocol.N_TON_BING_GET_RANK_REWARD, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret == 0) {
            return;
        }

        ChannelTonBingProto.CTonBingGetRankRewardRespMsg.Builder respMsg = ChannelTonBingProto.CTonBingGetRankRewardRespMsg.newBuilder();
        respMsg.setRet(ret);
        player.sendPacket(ClientProtocol.U_CTON_BING_GET_RANK_REWARD, respMsg);
    }
}
